<?php

  $query = "
    SELECT symbol_1, symbol_2, value, s1.name as s1_name, s2.name as s2_name
    FROM correlation_data
    JOIN stock s1 ON correlation_data.symbol_1 = s1.symbol
    JOIN stock s2 ON correlation_data.symbol_2 = s2.symbol
    ORDER BY value DESC
    LIMIT 5";
  
  $positive = array();
  if (($result = mysql_query($query)) && mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_assoc($result)) {
        $positive[] = $row;
      }
      array_reverse($positive);
   }
   
  $query = "
    SELECT symbol_1, symbol_2, value, s1.name as s1_name, s2.name as s2_name
    FROM correlation_data
    JOIN stock s1 ON correlation_data.symbol_1 = s1.symbol
    JOIN stock s2 ON correlation_data.symbol_2 = s2.symbol
    ORDER BY value
    LIMIT 5"; 

  $negative = array();
   if (($result = mysql_query($query)) && mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_assoc($result)) {
        $negative[] = $row;
      }
      array_reverse($negative);
   }

  $stocks = array(
    "direct" => $positive,
    "inverse" => $negative
  );


?>
      <div class="hero-unit">
        <h1>Welcome to Canoodle!</h1>
        <p>Have you ever wondered what stocks follow the same patterns or have strong correlations with one another?  Maybe you want to know what stocks do the exact opposite.  Introducing Canoodle, a new website that gives you a window into an often overlooked type of market analysis.  Updated daily.</p>
        <p><a class="btn btn-primary btn-large" href="/about">Learn more &raquo;</a></p>
      </div>
      <div class="row">
        <div class="span12">
          <form class="well form-search" action="/search">
            <input type="text" class="input-medium search-query" name="term">
            <button type="submit" class="btn">Search Stocks</button>
          </form>
        </div>
      </div>
      <div class="row">
 <?php
 
  foreach ($stocks as $type => $pairs) {
 
?>
        <div class="span6">
          <h2>Strongest <?php echo ucfirst($type); ?> Correlations</h2>
          <table class="table table-bordered table-striped">
            <tbody>
<?php
    $i=1;
    foreach ($pairs as $stock) {

?>
              <tr>
                <td><?php echo $i++; ?></td>
                <td>
                  <?php //echo sparkline($pair["symbols"][0][0], $pair["symbols"][1][0], 18); ?>
                  <a href="/details?symbol=<?php echo $stock["symbol_1"]; ?>"><?php echo $stock["symbol_1"]; ?></a>
                  <i class="icon-resize-horizontal"></i>
                  <a href="/details?symbol=<?php echo $stock["symbol_2"]; ?>"><?php echo $stock["symbol_2"]; ?></a>
                </td>
                <td class="<?php echo ($stock["value"] >= 0) ? "positive" : "negative"; ?>"><?php echo $stock["value"] * 100; ?>%</td>
              </tr>
<?php 

    }

?>
              <tr>
                <td colspan="3">
                  <a href="#">View more...</a>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
<?php
 
  }
 
 ?>
      </div>